package models

import (
	"gintest/database"
)
type ParentStoreClass struct{
	Id        int    `json:"id" gorm:"column:id;primary_key" form:"id"`
	Title string `json:"title" gorm:"column:title" form:"title"`
	Sort  int `json:"sort" gorm:"column:sort" form:"sort"`
}
func (ParentStoreClass) TableName() string {
	return "store_class"
}
//表结构
type StoreClass struct {
	Id        int    `json:"id" gorm:"column:id;primary_key" form:"id"`
	ParentId  int `json:"parent_id" gorm:"column:parent_id" form:"parent_id"`
	Title string `json:"title" gorm:"column:title" form:"title"`
	Sort  int `json:"sort" gorm:"column:sort" form:"sort"`

	ParentStoreClass ParentStoreClass `json:"parent_store_class" gorm:"ForeignKey:ParentId"`
}
func (StoreClass) TableName() string {
	return "store_class"
}

func GetStoreClassList(page uint64,pagesize uint64,keyword string)(data []StoreClass,total uint64){
	var offset=(page-1)*pagesize
	db:=database.SqlDB.Model(StoreClass{})

	if len(keyword)!=0{
		db=db.Where("title like ?","%"+keyword+"%")
	}

	db.Offset(offset).Limit(pagesize).Order("id desc").Preload("ParentStoreClass").Find(&data)
	db.Model(StoreClass{}).Count(&total)

	return data,total
}
func AddStoreClass(data *StoreClass) error{
	return database.SqlDB.Model(StoreClass{}).Omit("ParentStoreClass").Create(data).Error
}
func DelStoreClassByIds(ids []int) error{
	return database.SqlDB.Model(StoreClass{}).Where("id in (?)",ids).Delete(StoreClass{}).Error
}
func GetStoreClassById(id int) (data StoreClass){
	database.SqlDB.Model(StoreClass{}).Where("id=?",id).Preload("ParentStoreClass").First(&data);
	return data
}
func EditStoreClassById(id int,data interface{}) error{
	return database.SqlDB.Model(&StoreClass{}).Where("id=?",id).Updates(data).Error
}